/**
 * @author Kostya
 */

var iWindow = null;
var iOpener = null;

var iCurrentSession = null;
var iCurrentNoteRow = 0;

var iIDsArray  = [
	{
		name: 'agenda_id',
		caption: 'Agenda:'
	},{
		name: 'tasks_id',
		caption: 'New tasks:'
	},{
		name: 'notes_id',
		caption: 'New notes:'
	},{
		name: 'images_id',
		caption: 'New images:'
	},{
		name: 'files_id',
		caption: 'New files:'
	},{
		name: 'bookmarks_id',
		caption: 'New bookmarks:'
	}
];

var afterSelectNote = function(aNoteID){
	iCurrentSession[iIDsArray[iCurrentNoteRow].name] = aNoteID;
	updateNoteCaption(iCurrentNoteRow);
}

function setPanelHTML(aPanel, aHTML){
	if(aPanel.items){
		aPanel.remove(aPanel.items.get(0), true);		
	}
	aPanel.add({
		html: aHTML,
		border: false
	});
	aPanel.doLayout();
}

function updateNoteCaption(aIndex){
	var lItem = iIDsArray[aIndex];
	setPanelHTML(lItem.panel, '<img src="ext/resources/images/default/grid/loading.gif"/>Loading...');
	callAPI(iOpener.iSessionRecord.get('server'), 'GetNote', {
			noteid: iCurrentSession[lItem.name],
			token: iOpener.iToken
		}, function(aXML){
			var lHTML = '<img src="im/types/p'+aXML.documentElement.getAttribute('customIcon')+'.png"/>&nbsp;'+
					escapeHTML(aXML.documentElement.getAttribute('text'));
			setPanelHTML(lItem.panel, lHTML);			
		}, function(){
			setPanelHTML(lItem.panel, 'Error loading');
		});
}

 Ext.onReady(function(){
	iOpener = Ext.air.NativeWindow.getRootHtmlWindow();
	Ext.QuickTips.init();
	iWindow = window.nativeWindow;
	iOpener.iSessionWinReference = window;
	
	iCurrentSession = {};
	
    iCurrentSession.agenda_id = iOpener.iSessionRecord.get('agenda_id'); 
    iCurrentSession.notes_id = iOpener.iSessionRecord.get('notes_id');
    iCurrentSession.tasks_id = iOpener.iSessionRecord.get('tasks_id');
    iCurrentSession.images_id = iOpener.iSessionRecord.get('images_id');
    iCurrentSession.files_id = iOpener.iSessionRecord.get('files_id');
    iCurrentSession.bookmarks_id = iOpener.iSessionRecord.get('bookmarks_id');
    iCurrentSession.show_reminder = iOpener.iSessionRecord.get('show_reminder');
    iCurrentSession.show_reminder_before = iOpener.iSessionRecord.get('show_reminder_before');
    iCurrentSession.load_at_start = iOpener.iSessionRecord.get('load_at_start');
    iCurrentSession.update_interval = iOpener.iSessionRecord.get('update_interval');
    iCurrentSession.next_days = iOpener.iSessionRecord.get('next_days');
	
	iStatusBar = new Ext.StatusBar({
		defaultText: 'Idle',
		region: 'south'
	});
	
	var iItemsArray = [];
	iItemsArray[0] = {
		stateful: false,
		ctCls: 'cell-padding',
		html: '<b>Define root elements for:</b>',
		colspan: 3,
		border: false
	}
	for(var i = 0; i<iIDsArray.length; i++){
		var lItem = iIDsArray[i];
		var lPanel = new Ext.Panel({
			stateful: false,
			ctCls: 'x-table-layout cell-padding',
			border: false,
			height: 20
		});
		var lButton = new Ext.Button({
			stateful: false,
			style: 'padding-right: 5px;',
			text: 'Change',
			handler: function(){
				var lItem = iIDsArray[this.rowNumber];
				iCurrentNoteRow = this.rowNumber;
				iOpener.openNewWindow('selectNote', 'select_note.html?id='+
					iCurrentSession[lItem.name]);
			}
		});
		lButton.rowNumber = i;
		iItemsArray[iItemsArray.length] = {
			stateful: false,
			html: lItem.caption,
			width: 100,
			ctCls: 'cell-padding',
			border: false			
		}
		lItem.panel = lPanel;
		iItemsArray[iItemsArray.length] = lPanel;
		iItemsArray[iItemsArray.length] = lButton;		
	}
	
	var iShowReminder = new Ext.form.Checkbox({
		boxLabel: 'Show reminder for events&nbsp;',
		checked: iCurrentSession.show_reminder
	});
	
	var iReminderBefore = new Ext.form.NumberField({
		value: iCurrentSession.show_reminder_before,
		growMin: 1,
		width: 50
	});
	
	iReminderBefore.setDisabled(!iCurrentSession.show_reminder);
	
	iShowReminder.on('check', function(aItem, aState){
//		air.trace('value: '+aItem.getValue());
		iReminderBefore.setDisabled(!aItem.getValue());
	});
	
	var iUpdateInterval = new Ext.form.NumberField({
		value: parseInt(iCurrentSession.update_interval),
		growMin: 5,
		width: 50
	});
	
	var iNextDays = new Ext.form.NumberField({
		value: !iCurrentSession.next_days?3:iCurrentSession.next_days,
		growMin: 1,
		width: 50
	});
	
	var iReminderPanel = new Ext.Panel({
		stateful: false,
		width: 300,
		ctCls: 'cell-padding',
		layout: 'column',
		border: false,
		items:[
			{
				border: false,
				items: iShowReminder
			},{
				border: false,
				items: iReminderBefore
			},{
				style: 'padding-top: 5px;',
				border: false,
				html: '&nbsp;minutes'
			}
		]
	});
	
	var iUpdateIntervalPanel = new Ext.Panel({
		stateful: false,
		width: 300,
		ctCls: 'cell-padding',
		layout: 'column',
		border: false,
		items:[
			{
				style: 'padding-top: 5px;',
				border: false,
				html: 'Update agenda every&nbsp;'
			},{
				border: false,
				items: iUpdateInterval
			},{
				style: 'padding-top: 5px;',
				border: false,
				html: '&nbsp;minutes'
			}
		]
	});
	
	var iNextDaysPanel = new Ext.Panel({
		stateful: false,
		width: 300,
		ctCls: 'cell-padding',
		layout: 'column',
		border: false,
		items:[
			{
				style: 'padding-top: 5px;',
				border: false,
				html: 'Display agenda for next&nbsp;'
			},{
				border: false,
				items: iNextDays
			},{
				style: 'padding-top: 5px;',
				border: false,
				html: '&nbsp;days'
			}
		]
	});
	
	var iDefaultSession = new Ext.form.Checkbox({
		ctCls: 'cell-padding',
		boxLabel: 'Automatically log me in',
		checked: iCurrentSession.load_at_start
	});
	
	var iSaveButton = new Ext.Button({
		text: 'Save',
		handler: function(){
			
		    iOpener.iSessionRecord.set('agenda_id', iCurrentSession.agenda_id); 
		    iOpener.iSessionRecord.set('notes_id', iCurrentSession.notes_id);
		    iOpener.iSessionRecord.set('tasks_id', iCurrentSession.tasks_id);
		    iOpener.iSessionRecord.set('images_id', iCurrentSession.images_id);
		    iOpener.iSessionRecord.set('files_id', iCurrentSession.files_id);
		    iOpener.iSessionRecord.set('bookmarks_id', iCurrentSession.bookmarks_id);
		    
			iOpener.iSessionRecord.set('show_reminder', iShowReminder.getValue());
		    iOpener.iSessionRecord.set('show_reminder_before', iReminderBefore.getValue()<1?1:iReminderBefore.getValue());
		    iOpener.iSessionRecord.set('load_at_start', iDefaultSession.getValue());
		    iOpener.iSessionRecord.set('update_interval', iUpdateInterval.getValue()<5?5:iUpdateInterval.getValue());
		    iOpener.iSessionRecord.set('next_days', iNextDays.getValue()<0?0:iNextDays.getValue());
			if(iDefaultSession.getValue()){
				iOpener.sessions.each(function(record){
					if(iOpener.iSessionRecord.get('session_id')!=record.get('session_id'))
						record.set('load_at_start', false);
				});
			}
			iOpener.sessions.save();
			iWindow.close();
			iOpener.refreshAgenda(true);
		}
	});
	
	var iCancelButton = new Ext.Button({
		text: 'Cancel',
		handler: function(){
			iWindow.close();
		}
	});
	
	var iMainPanel = new Ext.Panel({		
		stateful: false,
		layout: 'table',
		layoutConfig: {
        	columns: 1
		},
		autoScroll: true,
		region: 'center',
		items: [{
			stateful: false,
			layout: 'table',
			border: false,
			layoutConfig: {
	        	columns: 3
			},
			items: iItemsArray
		}, iReminderPanel, iUpdateIntervalPanel, iNextDaysPanel, iDefaultSession],
		buttonAlign: 'center',
		buttons: [iSaveButton, iCancelButton]
	});
	
	var lViewport = new Ext.Viewport({
		layout: 'border',
		items: [iStatusBar, iMainPanel]
	})
	
	for (var i = 0; i < iIDsArray.length; i++) {
		updateNoteCaption(i);
	}
	
	iWindow.visible = true;
	iWindow.activate();
	
	lViewport.doLayout();
 })
